package com.auction.service.impl;

import cn.hutool.crypto.digest.MD5;
import com.auction.common.dao.MyBaseServiceImpl;
import com.auction.entity.User;
import com.auction.mapper.UserMapper;
import com.auction.service.UserService;
import com.auction.util.UpdatePassword;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserServiceImpl extends MyBaseServiceImpl<User, UserMapper> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Map selectPwd(String mobile) {
        return userMapper.selectPwd(mobile);
    }

    @Override
    public int updateStatusById(User user) {
        return userMapper.updateStatusById(user);
    }

    @Override
    public int updateMobile(User user) {
        if (user.getMobile()==null || user.getMobile().equals("")) return 0;
        if (user.getId()==null || user.getId().equals("")) return 0;

        String md5Mobile = MD5.create().digestHex(user.getMobile());
        String mobileCode = user.getMobileCode();
        String redisCode = stringRedisTemplate.opsForValue().get("mobileCode:"+md5Mobile);
        if (!mobileCode.equals(redisCode)) return -1;
        int rs = userMapper.updateUser(user);
        return rs;
    }

    @Override
    public int updatePassword(UpdatePassword updatePassword) {
        //对传递的数据进行判断
        if (updatePassword.getOldPassword()==null || updatePassword.getOldPassword().equals("")) return 0;
        if (updatePassword.getNewPassword()==null || updatePassword.getNewPassword().equals("")) return 0;
        if (updatePassword.getId()<=0) return 0;

        //校验手机验证码
        String mobile = updatePassword.getMobile();
        //手机号进行md5加密
        String md5Mobile = MD5.create().digestHex(mobile);

        String redisCode = stringRedisTemplate.opsForValue().get("mobileCode:"+md5Mobile);
        String mobileCode = updatePassword.getMobileCode();
        if (!mobileCode.equals(redisCode)) return -1;

        //对密码进行MD5加密
        String password = updatePassword.getNewPassword();
        String oldPassword = updatePassword.getOldPassword();
        updatePassword.setNewPassword(MD5.create().digestHex(password));
        updatePassword.setOldPassword(MD5.create().digestHex(oldPassword));

        //进行更新密码
        int rs = userMapper.updatePassword(updatePassword);

        return rs;
    }

    @Override
    public int updatePic(User user) {
        if (user.getPic()==null || user.getPic().equals("")) return 0;
        if (user.getId()==null || user.getId().equals("")) return 0;
        return userMapper.updateUser(user);
    }

    @Override
    public int updateName(User user) {
        if (user.getId()==null || user.getId().equals("")) return 0;
        return userMapper.updateUser(user);
    }

    @Override
    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    public int updateUserSex(User user) {

        if (user.getId()==null || user.getId().equals("")) return 0;
        int res = userMapper.updateUser(user);
        return res;
    }

    @Override
    public int saveEntity(User entity) {

        String mobileCode = entity.getMobileCode();
        String md5Mobile = MD5.create().digestHex(entity.getMobile());
        String redisMobileCode = stringRedisTemplate.opsForValue().get("mobileCode:"+md5Mobile);
        //redis中的短信验证
        if (redisMobileCode == null) return -1;
        //redis和前端传过来的code进行比较
        if (!mobileCode.equals(redisMobileCode)) return -1;

        //判断手机号是否已经注册
        int rs = userMapper.getMobile(entity.getMobile());
        if (rs >=1) return 0;

        //密码加密
        String password = entity.getPassword();
        entity.setPassword(MD5.create().digestHex(password));

        int res = super.saveEntity(entity);

        return res;
    }
}
